import 'dart:convert';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tab_list/custom/custom_slider.dart';
import 'package:flutter_tab_list/date/date.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LightBarModePage extends StatefulWidget {
  int currentIndex;
  bool isAutoScroll;
  bool iskey;
  List<ImageModel> lightBarList;
  LightBarModePage(
      {Key key,
        this.currentIndex,
        this.isAutoScroll,
        this.iskey,this.lightBarList})
      : super(key: key);

  @override
  _LightBarModePageState createState() => _LightBarModePageState();
}

class _LightBarModePageState extends State<LightBarModePage> {
  double progressValue = 180.0;
  double progressValue2 = 50.0;
  double progressValue3 = 50.0;
  Color color01;
  bool isPointColor = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Future<List<ImageModel>> getFromDesk() async {
    var sp = await SharedPreferences.getInstance();
    List<String> jsonStr = sp.getStringList(IMAGEMODEL_LIGHTBAR_LIST);
    if (jsonStr != null) {
      return jsonStr.map((e) => ImageModel.fromMap(json.decode(e))).toList();
    }
    print('获取成功');
    return List.empty();
  }
  void _removeStartLiat(ImageModel bean) {
    setState(() {
      if (widget.lightBarList.remove(bean)) {
        _updateList();
        print('删除成功');
      }
    });
  }
  void _updateList() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setStringList(
        IMAGEMODEL_LIGHTBAR_LIST, widget.lightBarList.map((e) => json.encode(e.toMap())).toList());
    print('更新成功');
  }

  void iskeyChange() {
    setState(() {
      widget.iskey = false;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height - 250,
      child: ListView(
        shrinkWrap: true,
        children: [
          Container(
            height: 150,
            child: Swiper(
              itemBuilder: (BuildContext context, int index) {
                return ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: Container(
                    color: Color.fromRGBO(27, 56, 96, 1),
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        Image.asset(
                          widget.lightBarList[index].image01,
                          color: widget.lightBarList[index].color01,
                          width: 200,
                          fit: BoxFit.fill,
                        ),
                        Image.asset(
                          widget.lightBarList[index].image02,
                          color: widget.lightBarList[index].color02,
                          width: 200,
                          fit: BoxFit.fill,
                        ),
                      ],
                    ),
                  ),
                );
              },
              key: widget.iskey ? ObjectKey(widget.currentIndex) : null,
              itemCount: widget.lightBarList.length,
              autoplay: widget.isAutoScroll,
              viewportFraction: 0.7,
              scale: 0.6,
              duration: 1000,
              autoplayDelay: 1500,
              autoplayDisableOnInteraction: true,
              index: widget.currentIndex,
              onIndexChanged: (int index) {
                setState(() {
                  widget.currentIndex = index;
                  print('index${index.toString}');
                  iskeyChange();
                });
              },
            ),
          ),
          SizedBox(height: 150),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '颜色',
                style: TextStyle(fontSize: 12, color: Colors.white),
              ),
              CustomSlider(
                  progressValue: progressValue,
                  onProgressValueChanged: (double value) {
                    setState(() {
                      progressValue = value;
                    });
                  },
                  onColorChanged: (Color value) {
                    setState(() {
                      color01 = value;
                      if (isPointColor) {
                        widget.lightBarList[widget.currentIndex].color02 = color01;
                        _updateList();
                      } else {
                        widget.lightBarList[widget.currentIndex].color01 = color01;
                        _updateList();
                      }
                    });
                  }),
              Text(
                '灵敏度',
                style: TextStyle(fontSize: 12, color: Colors.white),
              ),
              new Container(
                width:MediaQuery.of(context).size.width,
                alignment: Alignment.center,
                child: new Slider(
                  value: progressValue3,
                  inactiveColor: Colors.black12,
                  label: '${progressValue3}',
                  min: 0.0,
                  max: 100.0,
                  divisions: 1000,
                  activeColor: Colors.blue,
                  onChanged: (double) {
                    setState(() {
                      progressValue3 = double.roundToDouble();
                    });
                  },
                ),
              ),
              Text(
                '亮度',
                style: TextStyle(fontSize: 12, color: Colors.white),
              ),
              new Container(
                width:MediaQuery.of(context).size.width,
                alignment: Alignment.center,
                child: new Slider(
                  value: progressValue2,
                  inactiveColor: Colors.black12,
                  label: '${progressValue2}',
                  min: 0.0,
                  max: 100.0,
                  divisions: 1000,
                  activeColor: Colors.blue,
                  onChanged: (double) {
                    setState(() {
                      progressValue2 = double.roundToDouble();
                    });
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}